from typing import List

from sortedcontainers import SortedList


class Solution:
    def avoidFlood(self, rains: List[int]) -> List[int]:
        n =len(rains)
        ans = [-1] *n
        full_day ={}
        dry_day = SortedList()
        for i,lake in enumerate(rains):
            if lake == 0:
                ans[i]=1
                dry_day.add(i)
                continue
            if lake in full_day:
                last_full = full_day[lake]
                idx = dry_day.bisect_right(last_full)
                if idx==len(dry_day):
                    return []
                use_day = dry_day[idx]
                ans[use_day] = lake
                dry_day.discard(use_day)
            full_day[lake] = i
        return ans
